<p>Before a loop we put code that initializes all important
variables:</p>
<pre>
  1. nb = num_bits(n)
  2. p = 2 ** (nb - 1)
  3. bin = ''
</pre>
<p>After the initialization we then have the loop, with the following
header:</p>
<pre>
  1. for i in range(nb):
</pre>
<p>Inside the loop, there are typically two kinds of code: processing
code, and loop control code. In this case, the loop control code is
simple:</p>
<pre>
  1. p = p / 2
</pre>
<p>The processing code in this case decides what bit to the end of the
string being constructed:</p>
<pre>
  1. if n >= p:
  2.     bin += '1'
  3.     n = n - p
  4. else:
  5.     bin += '0'
</pre>